package org.fly.service.impl;

import org.fly.bean.Supplier;
import org.fly.bean.query.SupplierQuery;
import org.fly.contants.ErrorConstant;
import org.fly.contants.WebConstant;
import org.fly.dao.SupplierDao;
import org.fly.enums.AuditStatus;
import org.fly.enums.Status;
import org.fly.service.SmsRecordService;
import org.fly.service.SupplierService;
import org.fly.utils.CacheUtil;
import org.fly.utils.StrGenerator;
import org.fly.utils.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.fly.bean.common.BusinessException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by autoCode on 2018-01-31 17:19:50.
*/
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierDao supplierDao;
    @Autowired
    SmsRecordService smsRecordService;

    @Override
    public Supplier login(String telephone, String password) throws BusinessException{
        Supplier supplier = supplierDao.findByAccount(telephone);
        if(supplier == null ){
            throw new BusinessException(ErrorConstant.code_login_no_user, ErrorConstant.msg_login_no_user);
        }
        if(!supplier.getPassword().equals(password)){
            throw new BusinessException(ErrorConstant.code_login_password_error, ErrorConstant.msg_login_password_error);
        }
        fillCookieMap(supplier);
        CacheUtil.cache(supplier.getCookieMap().get(WebConstant.cookie_token),supplier);
        return supplier;
    }

    @Override
    public List<Supplier> list(SupplierQuery query) {
        return supplierDao.list(query);
    }

    @Override
    public long count(SupplierQuery query) {
        return supplierDao.count(query);
    }

    @Override
    public Supplier findById(String id) {
        return supplierDao.findById(id);
    }

    @Override
    public void save(Supplier bean)  throws BusinessException {

    if(StringUtils.isNotBlank(bean.getId())){
         supplierDao.update(bean);
    }else {
        bean.setId(UUIDFactory.random());
        bean.setCreateTime(System.currentTimeMillis());
        supplierDao.insert(bean);
    }
    }

    @Override
    public void register(Supplier data,String verifyCode) throws BusinessException {
        //检测账号是否存在
        data.setAccount(data.getAccount().trim());
        if(!smsRecordService.checkVerifyCode(data.getAccount(),verifyCode)){
            throw new BusinessException(ErrorConstant.ec_verify_fail,ErrorConstant.em_verify_fail);
        }
        Supplier old = supplierDao.findByAccount(data.getAccount());
        if(old != null) {
            throw new BusinessException(ErrorConstant.ec_account_exist, ErrorConstant.em_account_exist);
        }
        data.setId(UUIDFactory.random());
        data.setCreateTime(System.currentTimeMillis());

        data.setTelephone(data.getAccount());
        data.setStatus(Status.valid.name());
        data.setAuditStatus(AuditStatus.noInit.name());
        supplierDao.insert(data);
    }

    @Override
    public void delete(String id) {
        supplierDao.delete(id);
    }

    private void fillCookieMap(Supplier supplier){
        Map<String,String> cookieMap = new HashMap<>();
        cookieMap.put(WebConstant.cookie_fake, StrGenerator.generateRandomStr(32));
        cookieMap.put(WebConstant.cookie_name,StrGenerator.EncoderByMd5(supplier.getTelephone()));
        cookieMap.put(WebConstant.cookie_token,StrGenerator.EncoderByMd5(supplier.getTelephone()+"_"+supplier.getId()));
        supplier.setCookieMap(cookieMap);
    }
}
